import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/sub/MessageHistorySearchPage.dart';
import 'package:im_app/im/page/view/ListSeparator.dart';

import '../../pojo/ImPojo.dart';
import '../../util/ImConstant.dart';
import '../bloc/GroupAddCubit.dart';
import '../view/component/UserIconView.dart';
import 'GroupAddPage.dart';
import '../bloc/MessageHistoryCubit.dart';
import '../bloc/SessionSettingCubit.dart';
import 'SessionSettingBase.dart';

/// 消息用户管理
class SessionUserSettingPage extends SessionSettingBase {
  
  const SessionUserSettingPage({super.key});
  
  @override
  Widget build(BuildContext context) {
    var sessionSettingCubit = context.watch<SessionSettingCubit>();
    var session = sessionSettingCubit.session;
    return Scaffold(
      appBar: _buildAppBar(context),
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      body: SafeArea(
        left: false,
        right: false,
        child: Container(
          color: ImConstant.IM_RESERVE_BACKGROUND_COLOR,
          child: Material(
            color: ImConstant.IM_RESERVE_BACKGROUND_COLOR,
            child: Column(children: [
              _buildUserInfoPane(context, session),
              _buildSeparator(5),

              ListTile(title: const Text('查找聊天记录'), trailing: const Icon(Icons.keyboard_arrow_right), onTap: () {
                Navigator.of(context).push(MaterialPageRoute(builder: (context){
                  var messageHistoryCubit = BlocProvider.of<MessageHistoryCubit>(context);
                  messageHistoryCubit.add(MessageHistoryInitEvent(session.id!));
                  return MessageHistorySearchPage(session: session,);
                }));
              },),
              ListSeparator(indent: 10,),
              ListTile(title: const Text('消息免打扰'), trailing: Switch(
                value: !isDisturb(session),
                onChanged: (value){
                  sessionSettingCubit.add(SessionSettingDisturbEvent(!value));
                },),
              ),
              ListSeparator(indent: 10,),
              ListTile(title: const Text('置顶聊天'), trailing: Switch(
                value: isTop(session),
                onChanged: (value){
                  sessionSettingCubit.add(SessionSettingTopEvent(value));
                },)
              ),
              ListSeparator(indent: 10,),
              ListTile(title: const Text('提醒'), trailing: Switch(
                value: isWaking(session),
                onChanged: (value){},)),
              ListSeparator(indent: 10,),
              _buildSeparator(5),

              ListTile(title: const Text('设置当前聊天背景'), trailing: const Icon(Icons.keyboard_arrow_right), onTap: () {},),
              _buildSeparator(5),

              ListTile(title: const Text('清空聊天记录'), trailing: const Icon(Icons.keyboard_arrow_right), onTap: () {
                cleanMessages(context, session);
              },),
              _buildSeparator(5),

              ListTile(title: const Text('投诉'), trailing: const Icon(Icons.keyboard_arrow_right), onTap: () {},),
              Expanded(child: Container(color: ImConstant.IM_MAIN_BACKGROUND_COLOR,))
          ],),),
        ),
      ),
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(
      leading: InkWell(
        child: const Icon(Icons.chevron_left),
        onTap: () {
          Navigator.of(context).pop();
        },
      ),
      title: const Text(
        "聊天信息",
        style: TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE, fontWeight: FontWeight.w500),
      ),
      toolbarHeight: ImConstant.IM_APP_BAR_HEIGTH,
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      centerTitle: true,
    );
  }

  Widget _buildUserInfoPane(BuildContext context, ImSession session) {
    return Container(
      margin: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
      child: Row(
        children: [
          UserIconView(user: session.toUser!, iconSize: ImConstant.IM_USER_ICON_SIZE),
          Container(
            margin: const EdgeInsets.only(left: 20),
            width: 40,
            height: 40,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5), border: Border.all(width: 1, style: BorderStyle.solid)),
            child: InkWell(child: const Icon(Icons.add), onTap: () async {
              Navigator.of(context).push(MaterialPageRoute(builder: (context){
                return buildGroupCubit(session, const GroupAddPage());
              }));
            },),
          ),
        ],
      ),
    );
  }

  Widget _buildSeparator(double height) {
    return Container(height: height, color: ImConstant.IM_MAIN_BACKGROUND_COLOR,);
  }
}



